import apis from '../../api';
import ajax from './ajax';

let AjaxPlugin = {};
AjaxPlugin.install = Vue => {
  // 1. 添加全局方法或属性
  apis.forEach(item => {
    item.list &&
      item.list.forEach(apiItm => {
        Vue.prototype[`$$${apiItm.method}`] = function({
          headers = {},
          type = apiItm.type,
          path = apiItm.path,
          data = {},
          loading = true,
          opts = {},
          callback = null,
          extend = {},
        } = {}) {
          extend = {
            ...extend,
            ...apiItm,
          };
          return ajax.request.call(this, {
            headers,
            type,
            path,
            data,
            loading,
            opts,
            callback,
            extend,
          });
        };
      });
  });
};
export default AjaxPlugin;
